package com.jiangxfei.mymvp.app.home.mvp;

import com.jiangxfei.mymvp.app.home.bean.User;
import com.jiangxfei.mymvp.base.BaseModel;
import com.jiangxfei.mymvp.config.APIService;
import com.jiangxfei.mymvp.config.CacheService;
import com.jiangxfei.mymvp.net.IRepositoryManager;

import java.util.List;

import io.reactivex.Observable;
import io.rx_cache2.DynamicKey;
import io.rx_cache2.EvictDynamicKey;

/**
 * @author: JiangXFei
 * @date: 2018/1/9 0009
 * @content:
 */

public class HomeModel extends BaseModel implements HomeConstract.Model {
    private final int PAGESIZE = 10;

    public HomeModel(IRepositoryManager mRepositoryManager) {
        super(mRepositoryManager);
    }


    /**
     * 请求用户列表
     * <p>
     * eg:优先读取网络缓存,无缓存时进行网络请求
     *
     * @param since 起始id
     * @param cache 是否使用缓存
     * @return
     */
    @Override
    public Observable<List<User>> loadUser(int since, boolean cache) {
        return mRepositoryManager
                .obtainCacheService(CacheService.class)
                .getUsers(mRepositoryManager.obtainRetrofitService(APIService.class)
                                .getUsers(since, PAGESIZE)
                        , new DynamicKey(since)
                        , new EvictDynamicKey(cache))
                .map(listReply -> listReply.getData());
    }

//    @Override
//    public Observable<Object> test(HomeConstract.TestParam param) {
//        return mRepositoryManager
//                .obtainRetrofitService(APIService.class, APIUrl.API_URL1)
//                .test(param.feature, param.industryCode, param.lastSubCodeId, param.subIndustryCode);
//    }
//
//    @Override
//    public Observable<Object> login(HomeConstract.LoginParam param) {
//        return mRepositoryManager
//                .obtainRetrofitService(APIService.class, APIUrl.API_URL1)
//                .login(param.username,param.password);
//    }


}
